package com.wframe.project.jnd.util;

import com.wframe.project.jnd.entity.Promotion;
import com.wframe.project.jnd.rest.util.ClientException;
import com.wframe.project.jnd.rest.util.CodeEnum;
import com.wframe.project.jnd.util.PromotionBuilder;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created by lwb on 2017/6/3.
 */
@Component
public class PromotionBuilderFactory implements ApplicationContextAware {
    private static ApplicationContext appContext;

    public static PromotionBuilder get(String promotionType) throws ClientException {
        switch (promotionType) {
            case "FLASH_SALE":
                return (PromotionBuilder) appContext.getBean("flashSalePromotionBuilder");
            case "BUY_SEND":
                return (PromotionBuilder) appContext.getBean("buySendPromotionBuilder");
            case "MONEY_OFF":
                return (PromotionBuilder) appContext.getBean("moneyOffPromotionBuilder");
            case "PURCHASE_GIFT":
                return (PromotionBuilder) appContext.getBean("purchaseGiftPromotionBuilder");
            case "COMPLEX":
                return (PromotionBuilder) appContext.getBean("complexPromotionsBuilder");
            case "COMMON":
                return (PromotionBuilder) appContext.getBean("commonProductsBuilder");
            default:
                throw new ClientException(CodeEnum.Err);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        appContext = applicationContext;
    }

    public static Object getBean(String paramString) {
        return appContext.getBean(paramString);
    }
}
